#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

"""
@File    : exp_sheet_data.py
@Author  : different && 道阻且长,行则将至 行而不辍,未来可期
@Time    : 2021/10/19 14:55
@Desc    :

"""
import ast
from datetime import date

from expand.exp_data.exp_base_data import BaseData


def sheet_table_title():
    return [
        {"field": "count", "title": "序号", "fixed": "left", "width": 60, "align": "center"},
        {"field": "product_link", "title": "listing", "align": "center", "width": 100},
        {"field": "asin", "title": "Asin", "align": "center", "width": 120},
        {"field": "image_url", "title": "图片",
         "templet": "#showImg", "align": "center", "width": 100},
        {"field": "price", "title": "价格", "sort": True, "align": "center", "width": 80},
        {"field": "brand", "title": "品牌", "align": "center", "width": 100},
        {"field": "seller_type", "title": "分销方式", "width": 100, "sort": True, "align": "center"},
        {"field": "page_rank", "title": "页面排名", "width": 100, "sort": True, "align": "center"},
        {"field": "shelf_date", "title": "上架日期", "sort": True, "width": 100, "align": "center",
         "templet": "#dataFormat"},
        {"field": "add_days", "title": "上架天数", "width": 100, "align": "center"},
        {"field": "big_category", "title": "大类目", "align": "center", "width": 80},
        {"field": "big_category_rank", "title": "大类目排名", "width": 110, "sort": True, "align": "center"},
        {"field": "sub_category_1", "title": "小类目", "align": "center", "width": 80},
        {"field": "sub_category_1_rank", "title": "小类目排名", "width": 110, "sort": True, "align": "center"},
        {"field": "rank_days_30", "title": "近30天排名", "sort": True, "align": "center", "width": 110},
        {"field": "ratings", "title": "总评价", "width": 80, "align": "center"},
        {"field": "rating", "title": "评分", "width": 80, "sort": True, "align": "center"},
        {"field": "sale", "title": "月销量", "width": 80, "sort": True, "align": "center"},
        {"field": "sale_percentage", "title": "销量占比", "sort": True, "align": "center", "width": 100}
    ]


class SheetData(BaseData):
    def __init__(self, pk) -> None:
        super().__init__(pk)
        self.attr_ls = None
        self.cols = [
            {"field": "count", "title": "序号", "fixed": "left", "width": 60, "align": "center"},
            {"field": "product_link", "title": "listing", "align": "center", "width": 100},
            {"field": "asin", "title": "Asin", "align": "center", "width": 120},
            {"field": "image_url", "title": "图片",
             "templet": "#showImg", "align": "center", "width": 100},
            {"field": "price", "title": "价格", "sort": True, "align": "center", "width": 80},
            {"field": "brand", "title": "品牌", "align": "center", "width": 100},
            {"field": "seller_type", "title": "分销方式", "width": 100, "sort": True, "align": "center"},
            {"field": "page_rank", "title": "页面排名", "width": 100, "sort": True, "align": "center"},
            {"field": "shelf_date", "title": "上架日期", "sort": True, "width": 100, "align": "center",
             "templet": "#dataFormat"},
            {"field": "add_days", "title": "上架天数", "width": 100, "align": "center"},
            {"field": "big_category", "title": "大类目", "align": "center", "width": 80},
            {"field": "big_category_rank", "title": "大类目排名", "width": 110, "sort": True, "align": "center"},
            {"field": "sub_category_1", "title": "小类目", "align": "center", "width": 80},
            {"field": "sub_category_1_rank", "title": "小类目排名", "width": 110, "sort": True, "align": "center"},
            {"field": "rank_days_30", "title": "近30天排名", "sort": True, "align": "center", "width": 110},
            {"field": "ratings", "title": "总评价", "width": 80, "align": "center"},
            {"field": "rating", "title": "评分", "width": 80, "sort": True, "align": "center"},
            {"field": "sale", "title": "月销量", "width": 80, "sort": True, "align": "center"},
            {"field": "sale_percentage", "title": "销量占比", "sort": True, "align": "center", "width": 100}
        ]

    # 销量百分比
    def sale_percentage(self, sale):
        sale_percentage_result = self.conn.execute(self.sale_percentage_sql(sale)).first()[0]
        return sale_percentage_result

    def sheet_data(self):
        sheet_result = self.conn.execute(self.sheet_sql)
        result_list = []
        count = 1
        for i in sheet_result:
            d = dict(zip(i.keys(), i))
            attr = ast.literal_eval(d['attr'])
            ret = {**d, 'count': count, 'sale_percentage': self.sale_percentage(d['sale']), **attr,
                   'add_days': (date.today() - d['shelf_date']).days if (d['shelf_date']) else 0}
            self.attr_ls = list(attr.keys())
            result_list.append(ret)
            count += 1
        result_list.append(self.attr_ls)
        return result_list
